export default defineEventHandler(async (event) => {
  // 模拟商品数据
  const products = [
    {
      id: 1,
      name: 'MacBook Pro 14英寸',
      description: '搭载M2 Pro芯片的专业级笔记本电脑，适合开发者和创意工作者',
      price: 14999,
      originalPrice: 16999,
      category: '电子产品',
      brand: 'Apple',
      images: [
        'https://picsum.photos/400/300?random=1',
        'https://picsum.photos/400/300?random=2',
        'https://picsum.photos/400/300?random=3'
      ],
      stock: 25,
      rating: 4.8,
      reviewCount: 156,
      tags: ['笔记本电脑', 'M2芯片', '专业级'],
      specifications: {
        '处理器': 'M2 Pro',
        '内存': '16GB',
        '存储': '512GB SSD',
        '屏幕': '14英寸 Liquid Retina XDR'
      },
      status: 'in_stock',
      createdAt: '2024-01-15T10:00:00Z'
    },
    {
      id: 2,
      name: 'iPhone 15 Pro',
      description: '最新款iPhone，搭载A17 Pro芯片，支持USB-C接口',
      price: 7999,
      originalPrice: 8999,
      category: '电子产品',
      brand: 'Apple',
      images: [
        'https://picsum.photos/400/300?random=4',
        'https://picsum.photos/400/300?random=5'
      ],
      stock: 50,
      rating: 4.7,
      reviewCount: 234,
      tags: ['智能手机', 'A17芯片', 'USB-C'],
      specifications: {
        '处理器': 'A17 Pro',
        '存储': '128GB',
        '屏幕': '6.1英寸 Super Retina XDR',
        '摄像头': '48MP主摄'
      },
      status: 'in_stock',
      createdAt: '2024-02-01T14:30:00Z'
    },
    {
      id: 3,
      name: '机械键盘 Cherry MX',
      description: '德国原装Cherry MX轴体，PBT键帽，RGB背光',
      price: 599,
      originalPrice: 699,
      category: '电脑配件',
      brand: 'Cherry',
      images: [
        'https://picsum.photos/400/300?random=6',
        'https://picsum.photos/400/300?random=7'
      ],
      stock: 100,
      rating: 4.6,
      reviewCount: 89,
      tags: ['机械键盘', 'Cherry轴', 'RGB'],
      specifications: {
        '轴体': 'Cherry MX Red',
        '键帽': 'PBT材质',
        '背光': 'RGB',
        '连接': 'USB-C'
      },
      status: 'in_stock',
      createdAt: '2024-01-20T09:15:00Z'
    },
    {
      id: 4,
      name: '无线蓝牙耳机',
      description: '主动降噪，40小时续航，支持快充',
      price: 299,
      originalPrice: 399,
      category: '音频设备',
      brand: 'Sony',
      images: [
        'https://picsum.photos/400/300?random=8'
      ],
      stock: 0,
      rating: 4.5,
      reviewCount: 67,
      tags: ['蓝牙耳机', '降噪', '长续航'],
      specifications: {
        '降噪': '主动降噪',
        '续航': '40小时',
        '快充': '支持',
        '连接': '蓝牙5.0'
      },
      status: 'out_of_stock',
      createdAt: '2024-02-10T16:45:00Z'
    },
    {
      id: 5,
      name: '4K显示器 27英寸',
      description: 'IPS面板，HDR400，99% sRGB色域',
      price: 2499,
      originalPrice: 2999,
      category: '电脑配件',
      brand: 'LG',
      images: [
        'https://picsum.photos/400/300?random=9',
        'https://picsum.photos/400/300?random=10'
      ],
      stock: 15,
      rating: 4.9,
      reviewCount: 45,
      tags: ['4K显示器', 'IPS面板', 'HDR'],
      specifications: {
        '分辨率': '3840x2160',
        '面板': 'IPS',
        '色域': '99% sRGB',
        'HDR': 'HDR400'
      },
      status: 'in_stock',
      createdAt: '2024-01-25T11:20:00Z'
    }
  ]

  // 获取查询参数
  const query = getQuery(event)
  const page = parseInt(query.page as string) || 1
  const limit = parseInt(query.limit as string) || 10
  const category = query.category as string
  const brand = query.brand as string
  const minPrice = parseInt(query.minPrice as string)
  const maxPrice = parseInt(query.maxPrice as string)
  const search = query.search as string
  const sortBy = query.sortBy as string || 'createdAt'
  const sortOrder = query.sortOrder as string || 'desc'

  // 过滤数据
  let filteredProducts = products
  if (category) {
    filteredProducts = filteredProducts.filter(product => product.category === category)
  }
  if (brand) {
    filteredProducts = filteredProducts.filter(product => product.brand === brand)
  }
  if (minPrice) {
    filteredProducts = filteredProducts.filter(product => product.price >= minPrice)
  }
  if (maxPrice) {
    filteredProducts = filteredProducts.filter(product => product.price <= maxPrice)
  }
  if (search) {
    filteredProducts = filteredProducts.filter(product => 
      product.name.toLowerCase().includes(search.toLowerCase()) ||
      product.description.toLowerCase().includes(search.toLowerCase())
    )
  }

  // 排序
  filteredProducts.sort((a, b) => {
    let aValue = a[sortBy as keyof typeof a]
    let bValue = b[sortBy as keyof typeof b]
    
    if (typeof aValue === 'string' && typeof bValue === 'string') {
      return sortOrder === 'asc' ? aValue.localeCompare(bValue) : bValue.localeCompare(aValue)
    }
    
    if (typeof aValue === 'number' && typeof bValue === 'number') {
      return sortOrder === 'asc' ? aValue - bValue : bValue - aValue
    }
    
    return 0
  })

  // 分页
  const startIndex = (page - 1) * limit
  const endIndex = startIndex + limit
  const paginatedProducts = filteredProducts.slice(startIndex, endIndex)

  // 模拟延迟
  await new Promise(resolve => setTimeout(resolve, 250))

  return {
    code: 200,
    message: '获取商品列表成功',
    data: {
      products: paginatedProducts,
      pagination: {
        page,
        limit,
        total: filteredProducts.length,
        totalPages: Math.ceil(filteredProducts.length / limit)
      }
    }
  }
}) 